/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class NiveauanalyseJpaController implements Serializable {

    public NiveauanalyseJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public NiveauanalyseJpaController() {
    }
    
    
    
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Niveauanalyse niveauanalyse) {
        if (niveauanalyse.getCompteaCollection() == null) {
            niveauanalyse.setCompteaCollection(new ArrayList<Comptea>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Comptea> attachedCompteaCollection = new ArrayList<Comptea>();
            for (Comptea compteaCollectionCompteaToAttach : niveauanalyse.getCompteaCollection()) {
                compteaCollectionCompteaToAttach = em.getReference(compteaCollectionCompteaToAttach.getClass(), compteaCollectionCompteaToAttach.getIdCa());
                attachedCompteaCollection.add(compteaCollectionCompteaToAttach);
            }
            niveauanalyse.setCompteaCollection(attachedCompteaCollection);
            em.persist(niveauanalyse);
            for (Comptea compteaCollectionComptea : niveauanalyse.getCompteaCollection()) {
                Niveauanalyse oldNaOfCompteaCollectionComptea = compteaCollectionComptea.getNa();
                compteaCollectionComptea.setNa(niveauanalyse);
                compteaCollectionComptea = em.merge(compteaCollectionComptea);
                if (oldNaOfCompteaCollectionComptea != null) {
                    oldNaOfCompteaCollectionComptea.getCompteaCollection().remove(compteaCollectionComptea);
                    oldNaOfCompteaCollectionComptea = em.merge(oldNaOfCompteaCollectionComptea);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Niveauanalyse niveauanalyse) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Niveauanalyse persistentNiveauanalyse = em.find(Niveauanalyse.class, niveauanalyse.getIdNa());
            Collection<Comptea> compteaCollectionOld = persistentNiveauanalyse.getCompteaCollection();
            Collection<Comptea> compteaCollectionNew = niveauanalyse.getCompteaCollection();
            Collection<Comptea> attachedCompteaCollectionNew = new ArrayList<Comptea>();
            for (Comptea compteaCollectionNewCompteaToAttach : compteaCollectionNew) {
                compteaCollectionNewCompteaToAttach = em.getReference(compteaCollectionNewCompteaToAttach.getClass(), compteaCollectionNewCompteaToAttach.getIdCa());
                attachedCompteaCollectionNew.add(compteaCollectionNewCompteaToAttach);
            }
            compteaCollectionNew = attachedCompteaCollectionNew;
            niveauanalyse.setCompteaCollection(compteaCollectionNew);
            niveauanalyse = em.merge(niveauanalyse);
            for (Comptea compteaCollectionOldComptea : compteaCollectionOld) {
                if (!compteaCollectionNew.contains(compteaCollectionOldComptea)) {
                    compteaCollectionOldComptea.setNa(null);
                    compteaCollectionOldComptea = em.merge(compteaCollectionOldComptea);
                }
            }
            for (Comptea compteaCollectionNewComptea : compteaCollectionNew) {
                if (!compteaCollectionOld.contains(compteaCollectionNewComptea)) {
                    Niveauanalyse oldNaOfCompteaCollectionNewComptea = compteaCollectionNewComptea.getNa();
                    compteaCollectionNewComptea.setNa(niveauanalyse);
                    compteaCollectionNewComptea = em.merge(compteaCollectionNewComptea);
                    if (oldNaOfCompteaCollectionNewComptea != null && !oldNaOfCompteaCollectionNewComptea.equals(niveauanalyse)) {
                        oldNaOfCompteaCollectionNewComptea.getCompteaCollection().remove(compteaCollectionNewComptea);
                        oldNaOfCompteaCollectionNewComptea = em.merge(oldNaOfCompteaCollectionNewComptea);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = niveauanalyse.getIdNa();
                if (findNiveauanalyse(id) == null) {
                    throw new NonexistentEntityException("The niveauanalyse with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Niveauanalyse niveauanalyse;
            try {
                niveauanalyse = em.getReference(Niveauanalyse.class, id);
                niveauanalyse.getIdNa();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The niveauanalyse with id " + id + " no longer exists.", enfe);
            }
            Collection<Comptea> compteaCollection = niveauanalyse.getCompteaCollection();
            for (Comptea compteaCollectionComptea : compteaCollection) {
                compteaCollectionComptea.setNa(null);
                compteaCollectionComptea = em.merge(compteaCollectionComptea);
            }
            em.remove(niveauanalyse);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Niveauanalyse> findNiveauanalyseEntities() {
        return findNiveauanalyseEntities(true, -1, -1);
    }

    public List<Niveauanalyse> findNiveauanalyseEntities(int maxResults, int firstResult) {
        return findNiveauanalyseEntities(false, maxResults, firstResult);
    }

    private List<Niveauanalyse> findNiveauanalyseEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Niveauanalyse.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Niveauanalyse findNiveauanalyse(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Niveauanalyse.class, id);
        } finally {
            em.close();
        }
    }

    public int getNiveauanalyseCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Niveauanalyse> rt = cq.from(Niveauanalyse.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
